package com.xisan.practice.rabbitmq.utils;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class RabbitMQUtils {
    private static ConnectionFactory connectionFactory;
    static {
        //1.创建工厂
        connectionFactory = new ConnectionFactory();
        //2.设置属性
        connectionFactory.setHost("192.168.253.128");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/ems");
        connectionFactory.setUsername("ems");
        connectionFactory.setPassword("ems");
    }
    // 提供连接对象
    public static Connection getConnection(){
        try{
            //3.创建连接对象
            return connectionFactory.newConnection();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 关闭连接
    public static void close(Channel channel, Connection connection){
        try{
            if(channel != null){
                channel.close();
            }
            if(connection != null){
                connection.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
